﻿#region License
/*
Copyright (c) 2005-2012, CellAO Team

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the CellAO Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion
#region Usings...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
#endregion
#region NameSpace
namespace AssemblyExplorer
{

    #region Class AssemblyReader
    /// <summary>
    /// The Class in charge of printing information to our consoles
    /// </summary>
    class AssemblyReader
    {

        #region Fields
        Form callingForm;
        string assemblyFile;

        #endregion

        #region Properties
        public Assembly FileToRead
        {
            get
            {
                try
                {
                    return Assembly.LoadFile(assemblyFile);
                }
                catch { }
                return null;
            }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// 
        /// </summary>
        /// <param name="form">Our MainForm</param>
        /// <param name="file">The Assembly file to read</param>
        public AssemblyReader(Form form, string file)
        {
            callingForm = form;
            assemblyFile = file;
        }
        #endregion
        #region Class Methods
        public void ExploreAssembly()
        {
            AssemblyDisplay display; // Where we want to send all our info
            display = (AssemblyDisplay)callingForm;
            ListViewItem item = new ListViewItem();

            try
            {
                
            }
            catch
            {
            }
            if (FileToRead != null)
            {
                try
                {
                    //Ok its loaded look in it
                    foreach (Module m in FileToRead.GetModules()) // The Module name is the Assembly File name.
                    {
                        //Now look for all the classes or types
                        foreach (Type t in m.GetTypes())
                        {
                            //Send to display
                            if (callingForm is AssemblyDisplay)
                            {
                                display = (AssemblyDisplay)callingForm;
                                item.Name = t.Name;
                                display.displayListView.Items.Add(item.Name);

                            }

                            //Now look for all the Methods or functions.
                            foreach (MethodInfo mi in t.GetMethods())
                            {
                                //Add More
                            }
                        }
                    }
                    display.Show();
                }
                catch
                {
                }
            }
        }

        public string[] GetClassMethods(string className)
        {
            /* We gonna send lots of info to whoever calls us*/

            List<string> info = new List<string>();
            Type wantedClass;

            if (FileToRead != null)
            {
                wantedClass = FileToRead.GetType(className);
                foreach (Module m in FileToRead.GetModules())
                {
                    info.Add(m.Name.ToUpper() + "\n");
                    
                    foreach (Type t in m.GetTypes())
                    {
                        //Our Classes add more info to our string array
                        
                        if (t.Name == className)
                        {
                            //We found the class we want, build our list.
                            info.Add("Full NameSpace to this class =   " + t.Namespace + "." + t.Name + "\n");
                            info.Add("Public Fields in this class.\n");
                            foreach(FieldInfo fi in t.GetFields()) // Only returns Public fields
                            {
                                info.Add("Public " + fi.FieldType.ToString() + " " + fi.Name + ";");
                            }
                            info.Add("\n");
                            info.Add("Public methods contained in this class.\n");
                            foreach (MethodInfo mi in t.GetMethods())
                            {

                                info.Add(mi.GetBaseDefinition() + " -> Returns " + mi.ReturnParameter);

                            }

                        }
                    }
                }

            }

            // We good file loaded.
            //Find the class
            return info.ToArray();

        }
        /// <summary>
        /// Function to remove the namespace
        /// from all class files
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public string RemoveCharactersBeforeDot(string s)
        {
            String input = s;
            int index = input.IndexOf('.');
            if(index >= 0)
            {
                return input.Substring(index + 1);
            }
            //Hmm if we got here then it has no .'s in it so just return input
            return input;
        }

        private bool CanFileLoad()
        {
            try
            {
                FileToRead.GetModules();
            }
            catch (Exception cantLoad)
            {
                MessageBox.Show(cantLoad.ToString());
                return false;
            }
            return true;
        }
        #endregion
    }
    #endregion Class AssemblyReader
}
#endregion NameSpace